package com.ruoyi.web.controller.gxs;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gxs.domain.GxsMorality;
import com.ruoyi.gxs.service.IGxsMoralityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 思想品德变化Controller
 * 
 * @author 高晓松
 * @date 2024-01-03
 */
@RestController
@RequestMapping("/gxs/morality")
public class GxsMoralityController extends BaseController
{
    @Autowired
    private IGxsMoralityService gxsMoralityService;

    /**
     * 查询思想品德变化列表
     */
//    @PreAuthorize("@ss.hasPermi('gxs:morality:list')")
    @GetMapping("/list")
    public TableDataInfo list(GxsMorality gxsMorality)
    {
        startPage();
        List<GxsMorality> list = gxsMoralityService.selectGxsMoralityList(gxsMorality);
        return getDataTable(list);
    }

    /**
     * 导出思想品德变化列表
     */
//    @PreAuthorize("@ss.hasPermi('gxs:morality:export')")
    @Log(title = "思想品德变化", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GxsMorality gxsMorality)
    {
        List<GxsMorality> list = gxsMoralityService.selectGxsMoralityList(gxsMorality);
        ExcelUtil<GxsMorality> util = new ExcelUtil<GxsMorality>(GxsMorality.class);
        util.exportExcel(response, list, "思想品德变化数据");
    }

    /**
     * 获取思想品德变化详细信息
     */
//    @PreAuthorize("@ss.hasPermi('gxs:morality:query')")
    @GetMapping(value = "/{gxsId}")
    public AjaxResult getInfo(@PathVariable("gxsId") Long gxsId)
    {
        return success(gxsMoralityService.selectGxsMoralityByGxsId(gxsId));
    }

    /**
     * 新增思想品德变化
     */
//    @PreAuthorize("@ss.hasPermi('gxs:morality:add')")
    @Log(title = "思想品德变化", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GxsMorality gxsMorality)
    {
        return toAjax(gxsMoralityService.insertGxsMorality(gxsMorality));
    }

    /**
     * 修改思想品德变化
     */
//    @PreAuthorize("@ss.hasPermi('gxs:morality:edit')")
    @Log(title = "思想品德变化", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GxsMorality gxsMorality)
    {
        return toAjax(gxsMoralityService.updateGxsMorality(gxsMorality));
    }

    /**
     * 删除思想品德变化
     */
//    @PreAuthorize("@ss.hasPermi('gxs:morality:remove')")
    @Log(title = "思想品德变化", businessType = BusinessType.DELETE)
	@DeleteMapping("/{gxsIds}")
    public AjaxResult remove(@PathVariable Long[] gxsIds)
    {
        return toAjax(gxsMoralityService.deleteGxsMoralityByGxsIds(gxsIds));
    }


    /**
     * 我的思想品德变化列表
     */
//    @PreAuthorize("@ss.hasPermi('gxs:morality:list')")
    @GetMapping("/MyList")
    public TableDataInfo MyList(GxsMorality gxsMorality)
    {
        startPage();
        List<GxsMorality> list = gxsMoralityService.selectGxsMoralityList(gxsMorality);

        String username = getUsername();

        for (int i = 0 ; i < list.size(); i++){
            String userName = list.get(i).getUserName();
            if (!userName.equals(username)) {
                list.remove(i);
                i--;
            }
        }

        return getDataTable(list);
    }
}
